package com.zm.xdata.service.publics.poster.util;

import java.io.*;

/**
 * json工具类
 */
public class JsonUtils {

    /**
     * 读取json文件，返回json串
     * @param fileName
     * @return
     */
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 从resource下加载文件
     * @param fileName
     * @return
     */
    public static String readJsonResourceFile(String fileName) {
        String jsonStr = "";
        try {
            InputStream reader = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    public static void main(String... args) throws FileNotFoundException {

        System.out.println(System.getProperty("user.dir").replaceAll("\\\\","/"));
//        JSONObject jsonObject= JSON.parseObject(readJsonResourceFile("static/ueditor/jsp/config.json"));
//        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
//            if(entry.getKey().contains("UrlPrefix"))
//            System.out.println(entry.getKey()+":"+entry.getValue());
//        }

//        System.out.println(jsonObject.toJSONString());
    }
}
